package thinkingJava.chapter18;

import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import static util.Print.*;

public class Blips3 implements Externalizable {
	private int i;
	private String s;
	public Blips3(){
		print("Blips3 Constructor");
	}
	public Blips3(String x, int a) {
		print("Blips3(String x, int a)");
		s = x;
		i = a;
	}
	public String toString() {
		return s + i;
	}
	public void writeExternal(ObjectOutput out) throws IOException {
		print("Blips3.writeExternal");
		out.writeObject(s);
		out.writeInt(i);
	}
	public void readExternal(ObjectInput in) throws IOException,
			ClassNotFoundException {
		print("Blips3.readExternal");
		s = (String) in.readObject();
		i = in.readInt();
	}
	public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
		print("Constructing Objects : ");
		Blips3 b3 = new Blips3("A String", 47);
		print(b3);
		ObjectOutputStream out = new ObjectOutputStream(
				new FileOutputStream("Blips3.out"));
		print("Saving Object : ");
		out.writeObject(b3);
		out.close();
		ObjectInputStream in = new ObjectInputStream(
				new FileInputStream("Blips3.out"));
		print("Recovering Object : ");
		b3 = (Blips3) in.readObject();
		print(b3);
	}

}
